package admin.dao;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import admin.model.Admin;

import viewmodel.UserEditStatus;

import model.Connect;
import model.User;

public class UserDAO {
	public static User checkUser(String username, String password){
		User user = null;

		String sql = "SELECT * FROM eonl_member WHERE username like'"+username+"' AND password like'"+password+"'";
		try{
			Statement state = Connect.getConnection().createStatement();
			ResultSet result = state.executeQuery(sql);
			
			while(result.next()){
				user = new User(result.getString("username"), "", result.getString("email"), result.getString("avatar"));
				user.setId(result.getInt("id"));
				user.setLever(result.getInt("level"));
			}
			
			result.close();
			state.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return user;
	}
	
	public static List<User> getAllUser(){
		ArrayList<User> listUser = new ArrayList<User>();
		
		String sql = "SELECT * FROM eonl_member ";
		
		try{
			Statement state = Connect.getConnection().createStatement();
			ResultSet result = state.executeQuery(sql);
			
			while(result.next()){
				User user = new User(result.getString("username"), "", result.getString("email"), result.getString("avatar"));
				user.setId(result.getInt("id"));
				user.setLever(result.getInt("level"));
				
				listUser.add(user);
			}
			
			result.close();
			state.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return listUser;
	}

	public static void removeUser(User user) {
		String sql = "DELETE FROM `eonl_member` WHERE id=" + user.getId();
		try{
			Statement state = Connect.getConnection().createStatement();
			state.executeUpdate(sql);
			state.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	public static void updateUser(UserEditStatus ues) {
		
		String sql ="UPDATE `eonl_member`"
				+" SET username = '"+ues.getUser().getUsername()
				+"', email='"+ues.getUser().getEmail()
				+"', level="+ues.getLevel().getLevelCode()
				+", avatar='member/"+ues.getUser().getAvatar()
				+"' WHERE id="+ues.getUser().getId();
		try{
			Statement state = Connect.getConnection().createStatement();
			state.executeUpdate(sql);
			state.close();
		}catch (Exception e) {
			e.printStackTrace();
		}	
	}

	public static boolean checNewUsername(String newUsername, int id) {
		String sql;
		if(id != 0) {
			sql = "SELECT * FROM eonl_member WHERE username like'"+newUsername+"' AND id != "+id;
		} else {
			sql = "SELECT * FROM eonl_member WHERE username like'"+newUsername+"'";
		}
		try{
			Statement state = Connect.getConnection().createStatement();
			ResultSet result = state.executeQuery(sql);
			
			while(result.next()){
				return true;
			}
			
			result.close();
			state.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return true;
	}
	
	public static void registerUser(User user) {
		String sql = "INSERT INTO `eonl_member` (username,password,email,level,avatar) VALUES('"+user.getUsername()+"','"+user.getPassword()+"','"+user.getEmail()+"',3,'member/"+user.getAvatar()+"')";
		try{
			Statement state = Connect.getConnection().createStatement();
			state.executeUpdate(sql);
			state.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

}
